import Vue from 'vue'
import App from './App'
import store from './store'
import schema from 'async-validator'
import cookie from '@/utils/store/cookie'
import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp);
import { parseRoute, _router, parseQuery } from '@/utils'
import { VUE_APP_RESOURCES_URL, VUE_APP_API_URL } from '@/config'
Vue.config.productionTip = false
Vue.config.devtools = process.env.NODE_ENV !== 'production'

Vue.prototype.$validator = function(rule) {
  return new schema(rule)
}


// 判断市场常见的几种刘海屏机型
uni.getSystemInfo({
	success: function (res) {
		Vue.prototype.$statusBarHeight = res.statusBarHeight;
		Vue.prototype.$phoneModel = res.model;
		if (res.model.indexOf("iPhone") >= 0 && (res.statusBarHeight > 20)) {
		   Vue.prototype.$isFullScreen = true
		} else { //非刘海屏 
			Vue.prototype.$isFullScreen = false;
		}
	}
});
// #ifdef APP-PLUS
    let statusBarHeight = uni.getSystemInfoSync()['statusBarHeight']; // 安全区高度
	const systemInfo = uni.getSystemInfoSync();
	let htight = 0
	
	if(plus.navigator.hasNotchInScreen()){
		Vue.prototype.$isFullScreen = true;
		console.log('刘海屏')
		htight = statusBarHeight + 60;
	}else{ //非刘海屏 
		Vue.prototype.$isFullScreen = false;
		console.log('非刘海屏')
		htight = statusBarHeight + 54;
	}
	
	
	// if (systemInfo.platform === 'android') {
	//     htight = statusBarHeight + 44;
	// } else if (systemInfo.platform === 'ios') {
	//     htight = statusBarHeight + 60;
	// }
	
	Vue.prototype.$statusBarHeightApp = statusBarHeight;
	Vue.prototype.$total = htight;
	Vue.prototype.$platform = systemInfo.platform
// #endif


Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.$store = store

const app = new Vue({
  ...App,
  store,
})

Object.defineProperty(Vue.prototype, '$yrouter', {
  get() {
    return _router
  },
})

Object.defineProperty(Vue.prototype, '$yroute', {
  get() {
    return this._route
  },
})

Vue.prototype.$VUE_APP_API_URL = VUE_APP_API_URL

let deviceType = ''
// #ifdef APP-PLUS
// App平台编译的代码
deviceType = 'app'
Vue.prototype.$platform = uni.getSystemInfoSync().platform
// #endif

// #ifdef MP-WEIXIN
// 微信小程序编译的代码
deviceType = 'routine'
// #endif

// #ifdef H5
// H5编译的代码

import { wechat, clearAuthStatus, oAuth, auth, toAuth, pay, openAddress, openShareAll, openShareAppMessage, openShareTimeline, wechatEvevt, ready, wxShowLocation } from '@/libs/wechat'

import { isWeixin } from '@/utils'
const CACHE_KEY = 'clear_0.0.1'

if (!cookie.has(CACHE_KEY)) {
  cookie.clearAll()
  cookie.set(CACHE_KEY, 1)
}

var urlSpread = parseQuery()['spread']
if (urlSpread) {
  cookie.set('spread', urlSpread)
}

// #endif

// #ifdef H5
// 判断是否是微信浏览器
async function init() {
  if (isWeixin()) {
    deviceType = 'weixin'
    let wechatInit = wechat()
    if (wechatInit) {
      await oAuth()
    }
  } else {
    deviceType = 'weixinh5'
  }
}
init()
// #endif

Vue.prototype.$deviceType = deviceType

Vue.mixin({
  onLoad() {
    const { $mp } = this.$root
    this._route = parseRoute($mp)
  },
  onShow() {
    _router.app = this
    _router.currentRoute = this._route;
  },
  // 这里为了解决 .vue文件中 template 无法获取 VUE.prototype 绑定的变量
  computed: {
    $VUE_APP_RESOURCES_URL() {
      return VUE_APP_RESOURCES_URL
    },
  },
})

store.commit('updateDevicetype', deviceType)

app.$mount()
